package com.xmall.product.service.impl.other;

import com.xmall.define.dict.TradeStatusEnum;
import com.xmall.define.base.Result;
import com.xmall.define.dto.order.OrderDto;
import com.xmall.define.dto.pay.AlipayTradeDto;
import com.xmall.product.service.other.IOrderTradeService;
import com.xmall.product.service.remote.OrderServiceFeign;
import com.xmall.product.service.remote.PayServiceFeign;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class OrderTradeServiceImpl implements IOrderTradeService {

    @Resource
    private OrderServiceFeign orderServiceFeign;

    @Resource
    private PayServiceFeign payServiceFeign;

    /**
     * 库存恢复前检查
     */
    @Override
    public Boolean shouldExecuteStockRevert(Long orderId) {
        try {
            Result<OrderDto> orderResult = orderServiceFeign.info(orderId);
            if(null != orderResult && orderResult.callSuccess()) {
                OrderDto order = orderResult.getData();
                // 查询远程交易接口
                Result<AlipayTradeDto> tradeResult = payServiceFeign.tradeInfo(order.getOrderSn().toString(), order.getTradeNo());
                // 查询到订单，而且订单远程状态不符合，需要恢复库存
                if(tradeResult.callSuccess() && tradeResult.getData().getExists()
                        && TradeStatusEnum.TRADE_SUCCESS.getValue().equalsIgnoreCase(tradeResult.getData().getTradeStatus())) {
                    return false;
                }
            }
        }catch (Exception e) {}
        return true;
    }
}
